import { $wuxDialog } from '../../dist/index'
const api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    avatarUrl: "/images/portrait.png",
    nickName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfoFun();
  },
  login : function(e){
    var that = this
    wx.getUserProfile({
      desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        wx.setStorage({
          data: res.userInfo,
          key: 'userInfo',
        })
        // 保存用户信息到服务端
        wx.request({
          url: api.StorageUserInfo,
          data: {
            gender: res.userInfo.gender,
            nickName:res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl,
            country: res.userInfo.country
          },
          success: function (resu) {
            wx.setStorage({
              data: resu.data.data,
              key: 'userId',
            })
            console.log(res)
            that.setData({
              nickName:res.userInfo.nickName,
              avatarUrl: res.userInfo.avatarUrl,
              country: res.userInfo.country,
              show:false
            })
            wx.login({
              success: function(res) {
                if (res.code) {
                  wx.request({
                    url: api.addUserOpenId,
                    data: {
                      userId: resu.data.data,
                      code: res.code
                    },
                    success: function (result) {},
                    fail: function (error) {
                      console.log(error);
                    }
                  })
                  wx.hideLoading()
                } else {
                  console.log("error code " + res.errMsg);
                  wx.hideLoading()
                }
              }
            })
            
          }
        })
      },
      fail(error){
        console.log(error)
      }
    })
  },
  getUserInfoFun(){
    var that = this
    wx.getStorage({
      key: 'userInfo',
      success(res){
        that.setData({
          nickName:res.data.nickName,
          avatarUrl: res.data.avatarUrl,
          show:false
        })
      }
    })
  },
  loginoutFun(){
    var that = this
    $wuxDialog().confirm({
      resetOnClose: true,
      closable: true,
      title: '退出',
      content: '确定要退出吗',
      onConfirm(e) {
        wx.clearStorage({
          complete: (res) => {
            that.setData({
              avatarUrl:"/images/portrait.png",
              nickName:"用户昵称",
              show: true
            })
          },
        })
      },
      onCancel(e) {},
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})